﻿using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using XamarinAndroidSample.Droid.Utils;
using Newtonsoft.Json;
using XamarinAndroidSample.Model;

namespace XamarinAndroidSample.Droid
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, BottomNavigationView.IOnNavigationItemSelectedListener,View.IOnClickListener
    {
        TextView textMessage;
        Button btnGet;
        Handler handler;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            textMessage = FindViewById<TextView>(Resource.Id.message);
            btnGet = FindViewById<Button>(Resource.Id.btnGet);
            BottomNavigationView navigation = FindViewById<BottomNavigationView>(Resource.Id.navigation);
            navigation.SetOnNavigationItemSelectedListener(this);
            btnGet.SetOnClickListener(this);
        }
        public bool OnNavigationItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.navigation_home:
                    textMessage.SetText(Resource.String.title_home);
                    return true;
                case Resource.Id.navigation_dashboard:
                    textMessage.SetText(Resource.String.title_dashboard);
                    return true;
                case Resource.Id.navigation_notifications:
                    textMessage.SetText(Resource.String.title_notifications);
                    return true;
            }
            return false;
        }

        public void OnClick(View v)
        {
            if(v.Id==Resource.Id.btnGet)
            {
                handler = new Handler();
                ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.SetTitle("获取用户信息");
                progressDialog.SetMessage("正在获取用户信息，请稍等");
                progressDialog.Show();
                handler.Post(async () =>
                {
                    string  result = await ClientUtil.GetAsync("employees/1");
                    EmployeeModel model = JsonConvert.DeserializeObject<EmployeeModel>(result);
                    textMessage.Text = "用户名："+model.UserName+"\n密码"+model.PassWord;
                    progressDialog.Hide();
                });
            }
        }
    }
}

